package Utils;


import entity.SysAccounts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * @Description:密码工具类
 * @Author: liujh
 * @Date:
 * @UpdateUser: liujh
 * @Date: 2020/08/19$ 11:34$
 * @UpdateRemark:
 * @Version: 1.0
 */
@Slf4j
public class PasswordUtils {
	private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
	private static String algorithmName = "md5";
	private static int hashIterations = 2;

	public static void encryptPassword(SysAccounts user) {
		user.setSalt(randomNumberGenerator.nextBytes().toHex());
		String newPassword = new SimpleHash(algorithmName, user.getPassword(),  ByteSource.Util.bytes(user.getCredentialsSalt()), hashIterations).toHex();
		user.setPassword(newPassword);
	}

	public static String password(SysAccounts user){
		String encryptPassword = new SimpleHash(algorithmName,user.getPassword(),ByteSource.Util.bytes(user.getCredentialsSalt()),hashIterations).toHex();
		return encryptPassword;
	}


 /*随机字符
    String filename=RandomStringUtils.randomAlphanumeric(10);*/

	public static void main(String[] args) {
		SysAccounts user = new SysAccounts();
		user.setAccount("jok");
		user.setPassword("123456");
		encryptPassword(user);
	    log.info("salt:{}",user.getCredentialsSalt());
        ByteSource by =  ByteSource.Util.bytes(user.getCredentialsSalt());
        log.info("by:{}",by);
        log.info("user:{}",user);
     /*   log.info("pwd:{}",password(user));
        log.info("user:{}",user);*/

	}
}
